local sk__tiance = fk.CreateSkill {

  name = "sk__tiance",

  tags = {  },

}



sk__tiance:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__tiance.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__tiance-choose", sk__tiance.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    local judge = {
      who = target,
      reason = sk__tiance.name,
      pattern = ".",
    }
    room:judge(judge)
    if to.dead then return end
    if judge.card.suit ~= Card.NoSuit then
      local cards = table.filter(room.draw_pile, function (id)
        return Fk:getCardById(id).suit == judge.card.suit
      end)
      table.insertTable(cards, table.filter(room.discard_pile, function (id)
        return Fk:getCardById(id).suit == judge.card.suit
      end))
      for _, p in ipairs(room:getOtherPlayers(to)) do
        table.insertTable(cards, table.filter(p:getCardIds("he"), function (id)
          return Fk:getCardById(id).suit == judge.card.suit
        end))
      end
      if #cards > 0 then
        room:moveCardTo(table.random(cards, 2), Card.PlayerHand, to, fk.ReasonPrey, sk__tiance.name, nil, false, to)
      end
    end
    if not player.dead and event == fk.Damaged and data.card and data.card.color == judge.card.color then
      event:setCostData(self, "sk__jiexin")
    end
  end,
})

return sk__tiance